#include "../rt/shading_basics.h"
#include "phong_shaders.h"




class bumpPhongShader : public TexturedPhongShader
{
protected:
	//float2 m_texCoord; 

public:
	SmartPtr<Texture> bumpTexture;
	//SmartPtr<Texture> amibientTexture;
	//SmartPtr<Texture> specTexture;

	//virtual void setTextureCoord(const float2& _texCoord) { m_texCoord = _texCoord;}

	/*virtual float4 getAmbientCoefficient() const 
	{ 
		float4 ret = DefaultPhongShader::getAmbientCoefficient();

		if(amibientTexture.data() != NULL)
			ret = amibientTexture->sample(m_texCoord);

		return ret;
	}*/

	virtual Vector getNormal() const 
	{ 
		float intensity=1.f;
		//get 4 texCoords left right over under our point
		float2 _x0(m_texCoord.x-0.004f,m_texCoord.y),
			_x1(m_texCoord.x+0.004f,m_texCoord.y),
			_y0(m_texCoord.x,m_texCoord.y-0.004f),
			_y1(m_texCoord.x,m_texCoord.y+0.004f);
		//get the heigth map color for these four points
		float x0 = bumpTexture->sample(_x0).z,
			x1 = bumpTexture->sample(_x1).z,
			y0 = bumpTexture->sample(_y0).z,
			y1 = bumpTexture->sample(_y1).z;


		Vector posx(1,0,0),
				posy(0,-1,0);
		//get colour difference between left and right point
		float x0_x1 =x0-x1,
			//get colour difference between upper and lower point
			y0_y1 =y0-y1;

		//shift normal vector with colour differences
		Vector ret( m_normal+posx*intensity*((x0_x1)/2.f)+posy*intensity*((y0_y1)/2.f));
		return ret;


	}

	
	_IMPLEMENT_CLONE(bumpPhongShader);
};


class ProceduralShader : public DefaultPhongShader
{
protected:
	//float2 m_texCoord; 
	Point position;
public:

	float gapsize;

	virtual void getCoeff(float4 &_diffuseCoef, float4 &_specularCoef, float &_specularExponent) const
	{
		
		DefaultPhongShader::getCoeff(_diffuseCoef, _specularCoef, _specularExponent);
		// get specular Coef and Exponent
		_specularCoef=DefaultPhongShader::specularCoef;
		_specularExponent = specularExponent;

		//get positive position
		float x1,y1;
		x1=fabs(position.x);
		y1=fabs(position.y);

		//our scene is ten times bigger then the room with the cow
		x1=x1/10.f;
		y1=y1/10.f;

		//get value after dot
		x1=x1-(float)(int)x1;
		y1=y1-(float)(int)y1;

		//if((((x) % 16)>0)  &&  (((y) % 16)>0))
		if(x1>gapsize && y1>gapsize && x1<1.f-gapsize && y1<1.f-gapsize)
		{
			_diffuseCoef=float4(0.87f,0.843f,0.745f,0.f);
			
		}
		else
		{
					_diffuseCoef=float4::rep(0.3f);
		}

	}

	/*virtual void setTextureCoord(const float2& _texCoord) { 
	float x1,y1;
		x1=fabs(position.x);
		y1=fabs(position.y);
		x1=x1-(float)(int)x1;
		y1=y1-(float)(int)y1;

			m_texCoord.x=x1;
				m_texCoord.y=y1;
	}*/

	virtual void setPosition(const Point& _point) {
		position = _point;
		//std::cout<<position.x<<std::endl;
	}

	_IMPLEMENT_CLONE(ProceduralShader);

};

